package com.virtualmall.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.virtualmall.bean.Account;
import com.virtualmall.bean.Order;
import com.virtualmall.bean.ShoppingCart;
import com.virtualmall.common.Constants;
import com.virtualmall.common.Page;
import com.virtualmall.model.OrderModel;
import com.virtualmall.service.AccountService;
import com.virtualmall.service.OrderService;
import com.virtualmall.service.ShoppingCartService;

@Controller
@RequestMapping("/order")
public class OrderController {

    //private Logger logger = Logger.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private ShoppingCartService shoppingCartService;
    
    @Autowired
    private AccountService accountService;
    
    @RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> create(OrderModel orderModel,HttpServletRequest request){
        //logger.info("create() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            Date nowDate = new Date();
            Long userId = (Long)request.getSession().getAttribute(Constants.USER_ID_SESSION);
            
            Account currentUser = this.accountService.read(userId);
            if(StringUtils.isEmpty(currentUser.getAddress().getName())){
                result.put("result", Constants.FAILURE_RESULT_FLAG);
                result.put("errorCode", "addressInfoNull");
            }else{
                Account account = new Account();
                account.setId(userId);
                
                List<Long> shoppingCartIdList = orderModel.getShoppingCartIdList();
                for(Long cartId:shoppingCartIdList){
                    ShoppingCart sc = new ShoppingCart();
                    sc.setId(cartId);
                    this.shoppingCartService.delete(sc);
                }
                
                List<Order> orderList = orderModel.getOrderList();
                for(Order order : orderList){
                    order.setStatus("0");
                    order.setPayType("0");
                    order.setAccount(account);
                    order.setCreateDate(nowDate);
                    
                    this.orderService.create(order);
                }
                
                result.put("result", Constants.SUCCESS_RESULT_FLAG);
            }
        } catch (Exception e) {
            //logger.error("create() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("create() method end...");

        return result;
    }
    
    @RequestMapping(value="/list",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> list(String orderType,Page page,HttpServletRequest request){
        //logger.info("list() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
        	Account account = (Account)request.getSession().getAttribute(Constants.USER_ACCOUNT_SESSION);
        	
        	Long userId = null;
        	if(Constants.ACCOUNT_TYPE_CUSTOMER.equals(account.getAccountType())){
        		userId = account.getId();
        	}

            List<Order> orderList = this.orderService.getOrderList(userId,orderType,page);
            Long orderListCount = this.orderService.getOrderListCount(userId);

            for(Order order:orderList){
                String name = order.getAccount().getAddress()==null?order.getAccount().getUserName():order.getAccount().getAddress().getName();
                order.getProduct().setAccount(null);
                order.setName(name);
            }

            result.put("orderList", orderList);
            result.put("orderListCount", orderListCount);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("list() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("list() method end...");

        return result;
    }
    
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(Order order){
        //logger.info("delete() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
            this.orderService.delete(order);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("delete() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("delete() method end...");

        return result;
    }
    
    @RequestMapping(value="/update",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> update(Order order){
        //logger.info("update() method begin...");

        Map<String,String> result = new HashMap<String,String>();
        
        try {
        	order = this.orderService.read(order.getId());
        	order.setStatus("1");
        	
        	this.orderService.update(order);
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("update() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("update() method end...");

        return result;
    }
    
    
}
